﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Buccontrols;
using System.IO;

namespace PluginTry
{
    public partial class FormTry : PluginForm
    {
        DirectoryInfo currentDir;

        public FormTry()
        {
            // basic intitialize logic; created by the IDE (Visual Studio itself)
            InitializeComponent();
        }

        private void tbx_path_KeyPress(object sender, KeyPressEventArgs e)
        {
            // nothing jet, just an eventhandler; could be handled in the future
        }

        private void tbx_path_KeyUp(object sender, KeyEventArgs e)
        {
            // load dir on enter...
            if (e.KeyData == Keys.Enter)
            {
                LoadFolderFiles(new DirectoryInfo(tbx_path.Text));
            }
        }

        public ListView.ListViewItemCollection LoadFolderFiles(DirectoryInfo currentDir)
        {
            // load dir on enter...
            this.lvw_files.Items.Clear();

            foreach (FileInfo file in this.currentDir.GetFiles())
            {
                this.lvw_files.Items.Add(file.Name);
            }

            return this.lvw_files.Items;
        }

        public TreeNodeCollection PopulateTreeViewNode(DirectoryInfo popInfo)
        {
            // populate the a certain treeviewnode
            // create a root-node
            TreeNode retval = new TreeNode(popInfo.Name);
            retval.Name = popInfo.FullName;
            currentDir = popInfo;

            try
            {
                // add all foud directories to the root-node
                foreach (DirectoryInfo dir in this.currentDir.GetDirectories())
                {
                    TreeNode dirNode = new TreeNode(dir.Name);
                    dirNode.Name = dir.FullName;
                    dirNode.Nodes.Add("loading...");
                    retval.Nodes.Add(dirNode);
                }
            }
            catch (Exception ex)
            {
                // write errors to the console
                Options.WriteComment("(x) " + ex.Message);
            }

            // return the result
            return retval.Nodes;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // load all drives standard in the treeview
            this.tvw_folders.Nodes.Clear();

            foreach (string driveletter in Environment.GetLogicalDrives())
            {
                TreeNode tn = new TreeNode(driveletter);
                tn.Name = driveletter;
                tn.Nodes.Add("loading...");
                this.tvw_folders.Nodes.Add(tn);
            }
        }

        private void tvw_folders_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            // load directories into node on select
            e.Node.Nodes.Clear();
            TreeNodeCollection tnc = PopulateTreeViewNode(new DirectoryInfo(e.Node.Name));
            foreach (TreeNode tn in tnc)
            {
                e.Node.Nodes.Add(tn);
            }
        }
    }
}
